﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WeiboFilter.DAL;
using WeiboFilter.Models;
using WeiboFilter.BLL;

namespace WeiboFilter
{
    public partial class Filter : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["oauth_token"] == null && Session["customer"] == null)
            {
                Response.Write("<script>alert('请先登录！');window.location.href='Login.aspx';</script>");
            }
            else
            {
                if (!Page.IsPostBack)
                {
                    bindData();
                }
            }
        }

        private void bindData()
        {
            StatusBLL sB = new StatusBLL();
            //条数可更改
            repWeibo.DataSource = sB.GetTopStatusWithScore(5, Session["customer"].ToString());
            repWeibo.DataBind();
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            //会绑定新的数据
            bindData();
        }

        protected static string GetClientClick(object value)
        {
            return String.Format(
              "ShowStatus('{0}'); return false;",
              value.ToString());
        }

        protected void repWeibo_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            RepeaterItem item = e.Item;
            if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item)
            {
                Label lblState = (Label)item.FindControl("lblState");
                Stat state = (Stat)Enum.Parse(typeof(Stat), lblState.Text);
                Literal litRemark = (Literal)item.FindControl("litRemark");
                if (state == Stat.AutoFilter)
                {
                    litRemark.Text = "<p style='color:Red'>帮您过滤</p>";
                }
                else if (state == Stat.GuessYouLike)
                {
                    litRemark.Text = "<p style='color:Green'>猜你喜欢</p>";
                }
                else if (state == Stat.Dirty)
                {
                    Label lblScore = (Label)item.FindControl("lblScore");
                    int i = lblScore.Text.IndexOf('.');
                    if (i< 0)
                        litRemark.Text = "倾向分:" + lblScore.Text;
                    else
                    {
                        litRemark.Text = "倾向分:" + lblScore.Text.Substring(0, i + 2);
                    }
                }
            }

        }
    }
}